<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1">
<meta name="theme-color" content="#222">

<script>
    (function(){
        if(''){
            if (prompt('请输入文章密码') !== ''){
                alert('密码错误！');
                history.back();
            }
        }
    })();
</script>








<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">






  <script>
  (function(i,s,o,g,r,a,m){i["DaoVoiceObject"]=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;a.charset="utf-8";m.parentNode.insertBefore(a,m)})(window,document,"script",('https:' == document.location.protocol ? 'https:' : 'http:') + "//widget.daovoice.io/widget/6f697dad.js","daovoice")
  daovoice('init', {
      app_id: "6f697dad"
    });
  daovoice('update');
  </script>















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.ico?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="带你学会Java基础课程,">





  <link rel="alternate" href="/atom.xml" title="达叔小生" type="application/atom+xml">






<meta name="description" content="第80节:Java中的MVC设计模式">
<meta name="keywords" content="带你学会Java基础课程">
<meta property="og:type" content="article">
<meta property="og:title" content="第80节:Java中的MVC设计模式">
<meta property="og:url" content="https://huangguangda.github.io/2019/01/30/1/index.html">
<meta property="og:site_name" content="达叔小生">
<meta property="og:description" content="第80节:Java中的MVC设计模式">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-55e49c6e25ec6604.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-ca0b42a2f15064b8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-d446c322b1b80e9b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-b8d5445d8b43d6b6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-3592e4b99507c4a5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-d11add5ecea31ebd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-a4c00419da20c5ab.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-b1bfd6455610171a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-45251a153841a9d9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-f609771ec475ec19.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-618effe10e976b32.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-c660ad13183515c7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/11158618-9181932d3b49f07d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2019-02-01T13:32:18.789Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="第80节:Java中的MVC设计模式">
<meta name="twitter:description" content="第80节:Java中的MVC设计模式">
<meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/11158618-55e49c6e25ec6604.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":true,"onmobile":true},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://huangguangda.github.io/2019/01/30/1/">




<!-- 网页加载条 -->
<script src="https://neveryu.github.io/js/src/pace.min.js"></script>
  <title>第80节:Java中的MVC设计模式 | 达叔小生</title>
  









  <!-- 音频播放 -->
<link rel="stylesheet" href="/dist/APlayer.min.css">
<div id="aplayer"></div>
<script type="text/javascript" src="/dist/APlayer.min.js"></script>
<script type="text/javascript" src="/dist/music.js"></script>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <a href="https://github.com/huangguangda" class="github-corner" aria-label="View source on Github"><svg width="80" height="80" viewbox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">达叔小生</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">爱好分享的码农</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-首页">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-关于">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-标签">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-分类">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-归档">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-书籍">
          <a href="/books" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-book"></i> <br>
            
            书籍
          </a>
        </li>
      
        
        <li class="menu-item menu-item-电影">
          <a href="/movies" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-chrome"></i> <br>
            
            电影
          </a>
        </li>
      
        
        <li class="menu-item menu-item-站点">
          <a href="/sitemap.xml" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-sitemap"></i> <br>
            
            站点
          </a>
        </li>
      
        
        <li class="menu-item menu-item-公益">
          <a href="/404/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br>
            
            公益
          </a>
        </li>
      
        
        <li class="menu-item menu-item-课本">
          <a href="/book/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-apple"></i> <br>
            
            课本
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>


 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://huangguangda.github.io/2019/01/30/1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="达叔小生">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="达叔小生">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">第80节:Java中的MVC设计模式</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-01-30T21:29:06+08:00">
                2019-01-30
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/带你学会Java基础课程/" itemprop="url" rel="index">
                    <span itemprop="name">带你学会Java基础课程</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv">本文总阅读量
            <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>次
            </span>
          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  5,772
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  32
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p><img src="https://upload-images.jianshu.io/upload_images/11158618-55e49c6e25ec6604.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="标题图"></p>
<blockquote>
<p>第80节:Java中的MVC设计模式</p>
</blockquote>
<a id="more"></a>
<h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>了解java中的mvc模式.复习以及回顾!</p>
<p>事务,设置自动连接提交关闭.<br><code>setAutoCommit(false);</code><br><code>conn.commit();</code><br><code>conn.rollBack</code></p>
<p>隔离级别分别有:</p>
<p>读未提交有脏读<br>读已提交有不可重复读<br>可重复读有幻读<br>可串行化可以解决脏读,幻读,不可重复读</p>
<p>数据库连接池用于创建和管理连接对象.</p>
<p>DBCP和C3P0,分别了解代码设置和配置文件设置</p>
<p>DBUtils可以简化数据的增删改查.</p>
<p><code>QueryRunner runner = new QueryRunner();</code></p>
<p><code>runner.update();</code></p>
<p><code>runner.query();</code></p>
<blockquote>
<p>DBUtils通用的增删改</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">public void testInsert()&#123;</span><br><span class="line"> // 查询</span><br><span class="line"> Connection conn = null;</span><br><span class="line"> Statement st = null;</span><br><span class="line"> try&#123;</span><br><span class="line">  // 获取连接对象</span><br><span class="line"> conn = JDBCUtil.getConn();</span><br><span class="line"> // 根据连接对象,得到state ment</span><br><span class="line"> st = conn.createStatement();</span><br><span class="line"> // 执行添加</span><br><span class="line"> String sql = &quot;insert into t_stu values(null, &apos;dashu&apos;, 23)&quot;;</span><br><span class="line"> // 影响行数</span><br><span class="line"> int result = st.executeUpdate(sql);</span><br><span class="line"> if(result &gt; 0)&#123;</span><br><span class="line">  System.out.println(&quot;添加成功&quot;);</span><br><span class="line"> &#125;else&#123;</span><br><span class="line">  System.out.println(&quot;添加失败&quot;);</span><br><span class="line"> &#125;</span><br><span class="line">&#125;catch(Exception e)&#123;</span><br><span class="line"> e.printStackTrace();</span><br><span class="line">&#125;finally&#123;</span><br><span class="line"> JDBCUtil.release(conn, st);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>通用的增删改方法</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.commoncrud;</span><br><span class="line"></span><br><span class="line">import java.sql.Connection;</span><br><span class="line">import java.sql.PreparedStatement;</span><br><span class="line">import java.sql.SQLException;</span><br><span class="line"></span><br><span class="line">import org.junit.Test;</span><br><span class="line"></span><br><span class="line">import com.dashucoding.util.JDBCUtil;</span><br><span class="line">import com.dashucoding.util.JDBCUtil02;</span><br><span class="line"></span><br><span class="line">public class CommonCRUDUtil &#123;</span><br><span class="line">	</span><br><span class="line">	@Test</span><br><span class="line">	public void testUpdate() &#123;</span><br><span class="line">//		update(&quot;insert into account values(null, ?, ?)&quot; , &quot;dashu&quot;, 10);</span><br><span class="line">		</span><br><span class="line">//		update(&quot;delete from account where id = ?&quot;, 1);</span><br><span class="line">		</span><br><span class="line">		update(&quot;update account set money = ? where id = ?&quot;, 1999, 2);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	// 通用的增删改功能</span><br><span class="line">	public void update(String sql, Object ...args) &#123;</span><br><span class="line">		Connection conn = null;</span><br><span class="line">		PreparedStatement ps = null;</span><br><span class="line">		</span><br><span class="line">		try &#123;</span><br><span class="line">			conn = JDBCUtil02.getConn();</span><br><span class="line">			ps = conn.prepareStatement(sql);</span><br><span class="line">			for(int i = 0; i&lt;args.length; i++) &#123;</span><br><span class="line">				ps.setObject(i+1, args[i]);</span><br><span class="line">			&#125;</span><br><span class="line">			</span><br><span class="line">			ps.executeUpdate();</span><br><span class="line">			</span><br><span class="line">		&#125; catch (SQLException e) &#123;</span><br><span class="line">			// TODO Auto-generated catch block</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;finally &#123;</span><br><span class="line">			JDBCUtil.release(conn, ps);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>数据库的元数据</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">java.sql</span><br><span class="line">接口 DatabaseMetaData</span><br><span class="line">所有超级接口: Wrapper</span><br><span class="line"></span><br><span class="line">public interface DatabaseMetaData extends Wrapper</span><br><span class="line">数据库的整体综合信息</span><br></pre></td></tr></table></figure>
<p>方法</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">getCatalogs()</span><br><span class="line">可以获取在数据库中使用的类别名称</span><br><span class="line"></span><br><span class="line">getCatalogSeparator()</span><br><span class="line">获取此数据库用作类别和表名之间的分隔符的String</span><br><span class="line"></span><br><span class="line">getCatalogTerm()</span><br><span class="line">获取数据库供应商用于&quot;catalog&quot;的首选手语</span><br><span class="line"></span><br><span class="line">getClientInfoProperties()</span><br><span class="line">获取驱动程序支持的客户端信息属性的列表</span><br><span class="line"></span><br><span class="line">getConnection()</span><br><span class="line">获取此元数据对象所产生的连接</span><br><span class="line"></span><br><span class="line">getDatabaseMajorVersion()</span><br><span class="line">获取底层数据库的主版本号</span><br><span class="line"></span><br><span class="line">getDatabaseProductName()</span><br><span class="line">获取此数据库产品的名称</span><br><span class="line"></span><br><span class="line">getDatabaseProductVersion()</span><br><span class="line">获取此数据库产品的版本号</span><br><span class="line"></span><br><span class="line">getDefaultTransactionIsolation()</span><br><span class="line">获取此数据库的默认事务隔离级别</span><br><span class="line"></span><br><span class="line">getDriverMajorVersion()</span><br><span class="line">获取此JDBC驱动程序的主版本号</span><br><span class="line"></span><br><span class="line">getDriverMinorVersion()</span><br><span class="line">获取此JDBC驱动程序的次版本号</span><br><span class="line"></span><br><span class="line">getDriverName()</span><br><span class="line">获取此JDBC驱动程序的名称</span><br><span class="line"></span><br><span class="line">getDriverVersion()</span><br><span class="line">获取此JDBC驱动程序的String形式的版本号</span><br></pre></td></tr></table></figure>
<p>参数的元数据</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">java.sql</span><br><span class="line">接口 ParameterMetaData</span><br><span class="line">public interface ParameterMetaData extends Wrapper</span><br><span class="line">获取PreparedStatement对象中每个参数标记和属性信息的对象.</span><br></pre></td></tr></table></figure>
<p>方法的摘要</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">getParameterClassName(int param)</span><br><span class="line">获取Java类的完全限定名称</span><br><span class="line"></span><br><span class="line">getParameterCount()</span><br><span class="line">获取PreparedStatement对象中的参数数量</span><br><span class="line"></span><br><span class="line">getParameterMode(int param)</span><br><span class="line">获取指定参数的SQL类型</span><br><span class="line"></span><br><span class="line">getParameterTypeName(int param)</span><br><span class="line">获取指定参数的特定于数据库的类型名称</span><br><span class="line"></span><br><span class="line">getPrecision(int param)</span><br><span class="line">获取指定参数的指定两列大小</span><br><span class="line"></span><br><span class="line">getScale(int param)</span><br><span class="line">获取指定参数的小数点右边的位数</span><br><span class="line"></span><br><span class="line">isNullable(int param)</span><br><span class="line">获取是否允许在指定参数中使用null值</span><br><span class="line"></span><br><span class="line">isSigned(int param)</span><br><span class="line">获取指定参数的值是否可以是带符号的数字</span><br></pre></td></tr></table></figure>
<p>结果集元数据</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">java.sql</span><br><span class="line">接口 ResultSetMetaData</span><br><span class="line">public interface ResultSetMetaData extends Wrapper</span><br></pre></td></tr></table></figure>
<p>用来描述数据的数据,叫做元数据</p>
<p>数据库元数据,参数元数据,结果集元数据</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.commoncrud;</span><br><span class="line"></span><br><span class="line">import java.sql.Connection;</span><br><span class="line">import java.sql.ParameterMetaData;</span><br><span class="line">import java.sql.PreparedStatement;</span><br><span class="line">import java.sql.SQLException;</span><br><span class="line"></span><br><span class="line">import org.junit.Test;</span><br><span class="line"></span><br><span class="line">import com.dashucoding.util.JDBCUtil;</span><br><span class="line">import com.dashucoding.util.JDBCUtil02;</span><br><span class="line"></span><br><span class="line">public class CommonCRUDUtil &#123;</span><br><span class="line">	</span><br><span class="line">	@Test</span><br><span class="line">	public void testUpdate() &#123;</span><br><span class="line">//		update(&quot;insert into account values(null, ?, ?)&quot; , &quot;dashu&quot;, 10);</span><br><span class="line">		</span><br><span class="line">//		update(&quot;delete from account where id = ?&quot;, 1);</span><br><span class="line">		</span><br><span class="line">		update(&quot;update account set money = ? where id = ?&quot;, 1999, 2);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	// 通用的增删改功能</span><br><span class="line">	public void update(String sql, Object ...args) &#123;</span><br><span class="line">		Connection conn = null;</span><br><span class="line">		PreparedStatement ps = null;</span><br><span class="line">		</span><br><span class="line">		try &#123;</span><br><span class="line">			conn = JDBCUtil02.getConn();</span><br><span class="line">			ps = conn.prepareStatement(sql);</span><br><span class="line">			// 元数据</span><br><span class="line">			// 获取的有几个问好</span><br><span class="line">			ParameterMetaData metaData = ps.getParameterMetaData();</span><br><span class="line">			int count = metaData.getParameterCount();</span><br><span class="line">			</span><br><span class="line">			for(int i = 0; i&lt;count; i++) &#123;</span><br><span class="line">				ps.setObject(i+1, args[i]);</span><br><span class="line">			&#125;</span><br><span class="line">			</span><br><span class="line">			ps.executeUpdate();</span><br><span class="line">			</span><br><span class="line">		&#125; catch (SQLException e) &#123;</span><br><span class="line">			// TODO Auto-generated catch block</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;finally &#123;</span><br><span class="line">			JDBCUtil.release(conn, ps);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>TestDBUtils.java</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">// 删除</span><br><span class="line">queryRunner.update(&quot;delete from account where id = ?&quot;, 4);</span><br><span class="line">// 更新</span><br><span class="line">queryRunner.update(&quot;update account set money=? where id=?&quot;, 10000, 5);</span><br><span class="line"></span><br><span class="line">// 执行查询,查询到的数据还是在那个result里面然后调用下面的handle方法,由用户手动封装</span><br><span class="line">Account account = queryRunner.query(&quot;select * from account where id =?&quot;, new ResultSetHandler&lt;Account&gt;()&#123;</span><br><span class="line">@Override</span><br><span class="line"> public Account handle(ResultSet rs) throws SQLException&#123;</span><br><span class="line"> Account account = new Account();</span><br><span class="line"> while(rs.next())&#123;</span><br><span class="line">  String name = rs.getString(&quot;name&quot;);</span><br><span class="line">  int money = rs.getInt(&quot;money&quot;);</span><br><span class="line">  account.setName(name);</span><br><span class="line">  account.setMoney(money);</span><br><span class="line"> &#125;</span><br><span class="line">  return account;</span><br><span class="line"> &#125;</span><br><span class="line">&#125;, 6);</span><br></pre></td></tr></table></figure>
<blockquote>
<p>快速查询方法</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">@Test</span><br><span class="line">public void testQuery()&#123;</span><br><span class="line"> query(&quot;select * from account where id = ?&quot;, 接口的实现类对象, 3);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">class A implements ResultSetHandler &#123;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void handle(ResultSet rs) &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>通用的查询方法</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.commoncrud;</span><br><span class="line"></span><br><span class="line">import java.sql.Connection;</span><br><span class="line">import java.sql.ParameterMetaData;</span><br><span class="line">import java.sql.PreparedStatement;</span><br><span class="line">import java.sql.ResultSet;</span><br><span class="line">import java.sql.SQLException;</span><br><span class="line"></span><br><span class="line">import org.junit.Test;</span><br><span class="line"></span><br><span class="line">import com.dashucoding.domain.Account;</span><br><span class="line">import com.dashucoding.util.JDBCUtil;</span><br><span class="line">import com.dashucoding.util.JDBCUtil02;</span><br><span class="line"></span><br><span class="line">public class CommonCRUDUtil &#123;</span><br><span class="line">	class A implements ResultSetHandler&lt;Account&gt; &#123;</span><br><span class="line"></span><br><span class="line">		@Override</span><br><span class="line">		public Account handle(ResultSet rs) &#123;</span><br><span class="line">			// TODO Auto-generated method stub</span><br><span class="line">			try &#123;</span><br><span class="line">				Account account = new Account();</span><br><span class="line">				if(rs.next()) &#123;</span><br><span class="line">					String name = rs.getString(&quot;name&quot;);</span><br><span class="line">					int money = rs.getInt(&quot;money&quot;);</span><br><span class="line">					</span><br><span class="line">					account.setName(name);</span><br><span class="line">					account.setMoney(money);</span><br><span class="line">					</span><br><span class="line">				&#125;</span><br><span class="line">				return account;</span><br><span class="line">			&#125; catch (SQLException e) &#123;</span><br><span class="line">				// TODO Auto-generated catch block</span><br><span class="line">				e.printStackTrace();</span><br><span class="line">			&#125;</span><br><span class="line">			return null;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		/*@Override</span><br><span class="line">		public Object handle(ResultSet rs) &#123;</span><br><span class="line">			// TODO Auto-generated method stub</span><br><span class="line">			return null;</span><br><span class="line">		&#125;*/</span><br><span class="line"></span><br><span class="line">		/*@Override</span><br><span class="line">		public void handle(ResultSet rs) &#123;</span><br><span class="line">			// TODO Auto-generated method stub</span><br><span class="line">			try &#123;</span><br><span class="line">				while(rs.next()) &#123;</span><br><span class="line">					String name = rs.getString(&quot;name&quot;);</span><br><span class="line">					int money = rs.getInt(&quot;money&quot;);</span><br><span class="line">				&#125;</span><br><span class="line">			&#125; catch (SQLException e) &#123;</span><br><span class="line">				// TODO Auto-generated catch block</span><br><span class="line">				e.printStackTrace();</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;*/</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	@Test</span><br><span class="line">	public void testQuery() &#123;</span><br><span class="line">		/*query(&quot;select * from account where id=?&quot;, new ResultSetHandler() &#123;</span><br><span class="line"></span><br><span class="line">			@Override</span><br><span class="line">			public void handle(ResultSet rs) &#123;</span><br><span class="line">				// TODO Auto-generated method stub</span><br><span class="line">				</span><br><span class="line">			&#125;</span><br><span class="line">		    </span><br><span class="line">		&#125;,3);*/</span><br><span class="line">		</span><br><span class="line">		/*Account account = query(&quot;select * from account where id = ?&quot;, new A(), 2);</span><br><span class="line">		</span><br><span class="line">		System.out.println(account.toString());*/</span><br><span class="line">		</span><br><span class="line">		query(&quot;select * from account where id = ?&quot;, new ResultSetHandler&lt;Account&gt;() &#123;</span><br><span class="line"></span><br><span class="line">			@Override</span><br><span class="line">			public Account handle(ResultSet rs) &#123;</span><br><span class="line">				// TODO Auto-generated method stub</span><br><span class="line">				</span><br><span class="line">				return null;</span><br><span class="line">			&#125;&#125;, 2);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Test</span><br><span class="line">	public void testUpdate() &#123;</span><br><span class="line">//		update(&quot;insert into account values(null, ?, ?)&quot; , &quot;dashu&quot;, 10);</span><br><span class="line"></span><br><span class="line">//		update(&quot;delete from account where id = ?&quot;, 1);</span><br><span class="line"></span><br><span class="line">//		update(&quot;update account set money = ? where id = ?&quot;, 1999, 2);</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	public &lt;T&gt; T query(String sql, ResultSetHandler&lt;T&gt; handler, Object... args) &#123;</span><br><span class="line">		Connection conn = null;</span><br><span class="line">		PreparedStatement ps = null;</span><br><span class="line"></span><br><span class="line">		try &#123;</span><br><span class="line">			conn = JDBCUtil02.getConn();</span><br><span class="line">			ps = conn.prepareStatement(sql);</span><br><span class="line">			// 元数据</span><br><span class="line">			// 获取的有几个问好</span><br><span class="line">			ParameterMetaData metaData = ps.getParameterMetaData();</span><br><span class="line">			int count = metaData.getParameterCount();</span><br><span class="line"></span><br><span class="line">			for (int i = 0; i &lt; count; i++) &#123;</span><br><span class="line">				ps.setObject(i + 1, args[i]);</span><br><span class="line">			&#125;</span><br><span class="line"></span><br><span class="line">			// 执行查询工作,然后得到结果集</span><br><span class="line">			ResultSet rs = ps.executeQuery();</span><br><span class="line">			// 把结果集丢给调用者, 让它去封装数据,返回封装数据</span><br><span class="line">			T t = (T) handler.handle(rs);</span><br><span class="line">			return t;</span><br><span class="line">			</span><br><span class="line">			/*while(rs.next()) &#123;</span><br><span class="line">				rs.getInt(&quot;id&quot;);</span><br><span class="line">				rs.getString(&quot;name&quot;);</span><br><span class="line">				</span><br><span class="line">			&#125;*/</span><br><span class="line"></span><br><span class="line">		&#125; catch (SQLException e) &#123;</span><br><span class="line">			// TODO Auto-generated catch block</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125; finally &#123;</span><br><span class="line">			JDBCUtil.release(conn, ps);</span><br><span class="line">		&#125;</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	// 通用的增删改功能</span><br><span class="line">	/*public void update(String sql, Object... args) &#123;</span><br><span class="line">		Connection conn = null;</span><br><span class="line">		PreparedStatement ps = null;</span><br><span class="line"></span><br><span class="line">		try &#123;</span><br><span class="line">			conn = JDBCUtil02.getConn();</span><br><span class="line">			ps = conn.prepareStatement(sql);</span><br><span class="line">			// 元数据</span><br><span class="line">			// 获取的有几个问好</span><br><span class="line">			ParameterMetaData metaData = ps.getParameterMetaData();</span><br><span class="line">			int count = metaData.getParameterCount();</span><br><span class="line"></span><br><span class="line">			for (int i = 0; i &lt; count; i++) &#123;</span><br><span class="line">				ps.setObject(i + 1, args[i]);</span><br><span class="line">			&#125;</span><br><span class="line"></span><br><span class="line">			ps.executeUpdate();</span><br><span class="line"></span><br><span class="line">		&#125; catch (SQLException e) &#123;</span><br><span class="line">			// TODO Auto-generated catch block</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125; finally &#123;</span><br><span class="line">			JDBCUtil.release(conn, ps);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;*/</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.domain;</span><br><span class="line"></span><br><span class="line">public class Account &#123;</span><br><span class="line">	private String name;</span><br><span class="line">	private int money;</span><br><span class="line">	public String getName() &#123;</span><br><span class="line">		return name;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setName(String name) &#123;</span><br><span class="line">		this.name = name;</span><br><span class="line">	&#125;</span><br><span class="line">	public int getMoney() &#123;</span><br><span class="line">		return money;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setMoney(int money) &#123;</span><br><span class="line">		this.money = money;</span><br><span class="line">	&#125;</span><br><span class="line">	@Override</span><br><span class="line">	public String toString() &#123;</span><br><span class="line">		return &quot;Account [name=&quot; + name + &quot;, money=&quot; + money + &quot;]&quot;;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.commoncrud;</span><br><span class="line"></span><br><span class="line">import java.sql.ResultSet;</span><br><span class="line"></span><br><span class="line">public interface ResultSetHandler&lt;T&gt; &#123;</span><br><span class="line">	// 数据封装的规则,规范</span><br><span class="line">	T handle(ResultSet rs);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>JSP开发模式</p>
</blockquote>
<p>mvc的设计模式</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">javaBean数据的封装+jsp</span><br><span class="line">可以在jsp中直接写java代码</span><br><span class="line"></span><br><span class="line">&lt;%</span><br><span class="line"> 封装数据</span><br><span class="line"> 执行业务</span><br><span class="line"> 准备数据</span><br><span class="line">%&gt;</span><br><span class="line"></span><br><span class="line">特点维护比较难,jsp的页面代码会臃肿</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">servlet + javabean + jsp</span><br><span class="line"></span><br><span class="line">mvc模式:</span><br><span class="line"></span><br><span class="line">m:model</span><br><span class="line">模型层</span><br><span class="line">封装数据javabean</span><br><span class="line"></span><br><span class="line">v:view</span><br><span class="line">视图层</span><br><span class="line">jsp专注显示</span><br><span class="line"></span><br><span class="line">c:controller</span><br><span class="line">控制层</span><br><span class="line">servlet接收页面的请求,找模型层去处理</span><br></pre></td></tr></table></figure>
<blockquote>
<p>三层架构</p>
</blockquote>
<p>客户端,web层,业务逻辑层,数据访问层</p>
<p>servlet/jsp web层<br>javabean 业务逻辑层<br>dao 数据访问层</p>
<p>web层 对应 controller view</p>
<p>业务逻辑层 对应 model</p>
<p>数据访问层 对应 model</p>
<p>mvc模式: controller view model</p>
<p>controller: 接收请求,调用模型层出来数据,反馈给view</p>
<p>view: 用于显示</p>
<p>model: 数据的封装,数据的处理</p>
<blockquote>
<p>学生管理系统中的增删查改,分页,模糊查询</p>
</blockquote>
<h2 id="学生管理系统"><a href="#学生管理系统" class="headerlink" title="学生管理系统"></a>学生管理系统</h2><p>欢迎使用学生管理系统, 按姓名查询, 按性别查询, 添加<br>有编号,姓名 ,性别,电话,生日,爱好,简介,操作.</p>
<blockquote>
<p>创建数据库</p>
</blockquote>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-ca0b42a2f15064b8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="数据库"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-d446c322b1b80e9b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="ctrl+t"></p>
<blockquote>
<p>dao环境搭建</p>
</blockquote>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-b8d5445d8b43d6b6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="结构"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot;</span><br><span class="line">    pageEncoding=&quot;UTF-8&quot;%&gt;</span><br><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">&lt;meta charset=&quot;UTF-8&quot;&gt;</span><br><span class="line">&lt;title&gt;首页&lt;/title&gt;</span><br><span class="line">&lt;/head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line"></span><br><span class="line">&lt;h3&gt;&lt;a href=&quot;StudentListServlet&quot;&gt;显示所有学生列表&lt;/a&gt;&lt;/h3&gt;&lt;br&gt;</span><br><span class="line"></span><br><span class="line">&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.dao;</span><br><span class="line"></span><br><span class="line">import java.sql.SQLException;</span><br><span class="line">import java.util.List;</span><br><span class="line"></span><br><span class="line">import com.dashucoding.domain.Student;</span><br><span class="line"></span><br><span class="line">/*</span><br><span class="line"> * 这是针对学生表的数据访问</span><br><span class="line"> * </span><br><span class="line"> * */</span><br><span class="line">public interface StudentDao &#123;</span><br><span class="line">	</span><br><span class="line">	/*</span><br><span class="line">	 * 查询所有学生</span><br><span class="line">	 * list&lt;Student&gt;</span><br><span class="line">	 * */</span><br><span class="line">	List&lt;Student&gt; findAll() throws SQLException;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.dao.impl;</span><br><span class="line"></span><br><span class="line">import java.sql.Connection;</span><br><span class="line">import java.sql.SQLException;</span><br><span class="line">import java.util.List;</span><br><span class="line"></span><br><span class="line">import org.apache.commons.dbutils.QueryRunner;</span><br><span class="line">import org.apache.commons.dbutils.handlers.BeanListHandler;</span><br><span class="line"></span><br><span class="line">import com.dashucoding.dao.StudentDao;</span><br><span class="line">import com.dashucoding.domain.Student;</span><br><span class="line">import com.dashucoding.util.JDBCUtil02;</span><br><span class="line"></span><br><span class="line">/*</span><br><span class="line"> * 这是studentdao的实现,针对前面定义的规范,做出具体的实现</span><br><span class="line"> * */</span><br><span class="line">public class StudentDaoImpl implements StudentDao &#123;</span><br><span class="line">	/*</span><br><span class="line">	 * 查询所有学生</span><br><span class="line">	 * */</span><br><span class="line">	@Override</span><br><span class="line">	public List&lt;Student&gt; findAll() throws SQLException &#123;</span><br><span class="line">		QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource());</span><br><span class="line">		String sql = &quot;select * from stu&quot;;</span><br><span class="line">		List&lt;Student&gt; list = runner.query(sql, new BeanListHandler&lt;Student&gt;(Student.class));</span><br><span class="line">		return list;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.domain;</span><br><span class="line"></span><br><span class="line">import java.util.Date;</span><br><span class="line"></span><br><span class="line">/*</span><br><span class="line"> * 这是学生封装的对象bean</span><br><span class="line"> * 根据表写</span><br><span class="line"> * */</span><br><span class="line">public class Student &#123;</span><br><span class="line">	private int sid;</span><br><span class="line">	private String sname;</span><br><span class="line">	private String gender;</span><br><span class="line">	private String phone;</span><br><span class="line">	private String hobby;</span><br><span class="line">	private String info;</span><br><span class="line">	private Date birthday;</span><br><span class="line">	public int getSid() &#123;</span><br><span class="line">		return sid;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setSid(int sid) &#123;</span><br><span class="line">		this.sid = sid;</span><br><span class="line">	&#125;</span><br><span class="line">	public String getSname() &#123;</span><br><span class="line">		return sname;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setSname(String sname) &#123;</span><br><span class="line">		this.sname = sname;</span><br><span class="line">	&#125;</span><br><span class="line">	public String getGender() &#123;</span><br><span class="line">		return gender;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setGender(String gender) &#123;</span><br><span class="line">		this.gender = gender;</span><br><span class="line">	&#125;</span><br><span class="line">	public String getPhone() &#123;</span><br><span class="line">		return phone;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setPhone(String phone) &#123;</span><br><span class="line">		this.phone = phone;</span><br><span class="line">	&#125;</span><br><span class="line">	public String getHobby() &#123;</span><br><span class="line">		return hobby;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setHobby(String hobby) &#123;</span><br><span class="line">		this.hobby = hobby;</span><br><span class="line">	&#125;</span><br><span class="line">	public String getInfo() &#123;</span><br><span class="line">		return info;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setInfo(String info) &#123;</span><br><span class="line">		this.info = info;</span><br><span class="line">	&#125;</span><br><span class="line">	public Date getBirthday() &#123;</span><br><span class="line">		return birthday;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setBirthday(Date birthday) &#123;</span><br><span class="line">		this.birthday = birthday;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.servlet;</span><br><span class="line"></span><br><span class="line">import java.io.IOException;</span><br><span class="line">import javax.servlet.ServletException;</span><br><span class="line">import javax.servlet.http.HttpServlet;</span><br><span class="line">import javax.servlet.http.HttpServletRequest;</span><br><span class="line">import javax.servlet.http.HttpServletResponse;</span><br><span class="line"></span><br><span class="line">public class StudentListServlet extends HttpServlet &#123;</span><br><span class="line"></span><br><span class="line">	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException &#123;</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException &#123;</span><br><span class="line">		doGet(request, response);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.util;</span><br><span class="line"></span><br><span class="line">import java.io.FileInputStream;</span><br><span class="line">import java.io.InputStream;</span><br><span class="line">import java.sql.Connection;</span><br><span class="line">import java.sql.DriverManager;</span><br><span class="line">import java.sql.ResultSet;</span><br><span class="line">import java.sql.SQLException;</span><br><span class="line">import java.sql.Statement;</span><br><span class="line">import java.util.Properties;</span><br><span class="line"></span><br><span class="line">import javax.sql.DataSource;</span><br><span class="line"></span><br><span class="line">import com.mchange.v2.c3p0.ComboPooledDataSource;</span><br><span class="line"></span><br><span class="line">public class JDBCUtil02 &#123;</span><br><span class="line">	</span><br><span class="line">	static ComboPooledDataSource dataSource = null;</span><br><span class="line"></span><br><span class="line">	static &#123;</span><br><span class="line">		dataSource = new ComboPooledDataSource();</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	public static DataSource getDataSource() &#123;</span><br><span class="line">		return dataSource;</span><br><span class="line">	&#125;</span><br><span class="line">	/**</span><br><span class="line">	 * 获取连接对象</span><br><span class="line">	 * @return</span><br><span class="line">	 * @throws SQLException </span><br><span class="line">	 */</span><br><span class="line">	public static Connection getConn() throws SQLException&#123;</span><br><span class="line">		</span><br><span class="line">		return dataSource.getConnection();</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	/**</span><br><span class="line">	 * 释放资源</span><br><span class="line">	 * @param conn</span><br><span class="line">	 * @param st</span><br><span class="line">	 * @param rs</span><br><span class="line">	 */</span><br><span class="line">	public static void release(Connection conn , Statement st , ResultSet rs)&#123;</span><br><span class="line">		closeRs(rs);</span><br><span class="line">		closeSt(st);</span><br><span class="line">		closeConn(conn);</span><br><span class="line">	&#125;</span><br><span class="line">	public static void release(Connection conn , Statement st)&#123;</span><br><span class="line">		closeSt(st);</span><br><span class="line">		closeConn(conn);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	</span><br><span class="line">	private static void closeRs(ResultSet rs)&#123;</span><br><span class="line">		try &#123;</span><br><span class="line">			if(rs != null)&#123;</span><br><span class="line">				rs.close();</span><br><span class="line">			&#125;</span><br><span class="line">		&#125; catch (SQLException e) &#123;</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;finally&#123;</span><br><span class="line">			rs = null;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	private static void closeSt(Statement st)&#123;</span><br><span class="line">		try &#123;</span><br><span class="line">			if(st != null)&#123;</span><br><span class="line">				st.close();</span><br><span class="line">			&#125;</span><br><span class="line">		&#125; catch (SQLException e) &#123;</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;finally&#123;</span><br><span class="line">			st = null;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	private static void closeConn(Connection conn)&#123;</span><br><span class="line">		try &#123;</span><br><span class="line">			if(conn != null)&#123;</span><br><span class="line">				conn.close();</span><br><span class="line">			&#125;</span><br><span class="line">		&#125; catch (SQLException e) &#123;</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;finally&#123;</span><br><span class="line">			conn = null;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>service层</p>
</blockquote>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-3592e4b99507c4a5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="显示"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-d11add5ecea31ebd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="显示"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">dao只做一件事,数据操作层</span><br><span class="line">service是业务层</span><br></pre></td></tr></table></figure>
<blockquote>
<p>查询数据</p>
</blockquote>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-a4c00419da20c5ab.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="结构"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.dao;</span><br><span class="line"></span><br><span class="line">import java.sql.SQLException;</span><br><span class="line">import java.util.List;</span><br><span class="line"></span><br><span class="line">import com.dashucoding.domain.Student;</span><br><span class="line"></span><br><span class="line">/*</span><br><span class="line"> * 这是针对学生表的数据访问</span><br><span class="line"> * </span><br><span class="line"> * */</span><br><span class="line">public interface StudentDao &#123;</span><br><span class="line">	</span><br><span class="line">	/*</span><br><span class="line">	 * 查询所有学生</span><br><span class="line">	 * list&lt;Student&gt;</span><br><span class="line">	 * */</span><br><span class="line">	List&lt;Student&gt; findAll() throws SQLException;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.dao.impl;</span><br><span class="line"></span><br><span class="line">import java.sql.Connection;</span><br><span class="line">import java.sql.SQLException;</span><br><span class="line">import java.util.List;</span><br><span class="line"></span><br><span class="line">import org.apache.commons.dbutils.QueryRunner;</span><br><span class="line">import org.apache.commons.dbutils.handlers.BeanListHandler;</span><br><span class="line"></span><br><span class="line">import com.dashucoding.dao.StudentDao;</span><br><span class="line">import com.dashucoding.domain.Student;</span><br><span class="line">import com.dashucoding.util.JDBCUtil02;</span><br><span class="line"></span><br><span class="line">/*</span><br><span class="line"> *这是StudentDao的实现,针对前面定义的规范,做出具体的实现</span><br><span class="line"> * */</span><br><span class="line">public class StudentDaoImpl implements StudentDao &#123;</span><br><span class="line">	/*</span><br><span class="line">	 * 查询所有学生</span><br><span class="line">	 * */</span><br><span class="line">	@Override</span><br><span class="line">	public List&lt;Student&gt; findAll() throws SQLException &#123;</span><br><span class="line">		QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource());</span><br><span class="line">		return runner.query(&quot;select * from stu&quot;, new BeanListHandler&lt;Student&gt;(Student.class));</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.domain;</span><br><span class="line"></span><br><span class="line">import java.util.Date;</span><br><span class="line"></span><br><span class="line">/*</span><br><span class="line"> * 这是学生封装的对象bean</span><br><span class="line"> * </span><br><span class="line"> * */</span><br><span class="line">public class Student &#123;</span><br><span class="line">	</span><br><span class="line">	private int sid;</span><br><span class="line">	private String sname;</span><br><span class="line">	private String gender;</span><br><span class="line">	private String phone;</span><br><span class="line">	private String hobby;</span><br><span class="line">	private String info;</span><br><span class="line">	private Date birthday;</span><br><span class="line">	</span><br><span class="line">	public Student() &#123;</span><br><span class="line">		super();</span><br><span class="line">		// TODO Auto-generated constructor stub</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public Student(int sid, String sname, String gender, String phone, String hobby, String info, Date birthday) &#123;</span><br><span class="line">		super();</span><br><span class="line">		this.sid = sid;</span><br><span class="line">		this.sname = sname;</span><br><span class="line">		this.gender = gender;</span><br><span class="line">		this.phone = phone;</span><br><span class="line">		this.hobby = hobby;</span><br><span class="line">		this.info = info;</span><br><span class="line">		this.birthday = birthday;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	public int getSid() &#123;</span><br><span class="line">		return sid;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setSid(int sid) &#123;</span><br><span class="line">		this.sid = sid;</span><br><span class="line">	&#125;</span><br><span class="line">	public String getSname() &#123;</span><br><span class="line">		return sname;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setSname(String sname) &#123;</span><br><span class="line">		this.sname = sname;</span><br><span class="line">	&#125;</span><br><span class="line">	public String getGender() &#123;</span><br><span class="line">		return gender;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setGender(String gender) &#123;</span><br><span class="line">		this.gender = gender;</span><br><span class="line">	&#125;</span><br><span class="line">	public String getPhone() &#123;</span><br><span class="line">		return phone;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setPhone(String phone) &#123;</span><br><span class="line">		this.phone = phone;</span><br><span class="line">	&#125;</span><br><span class="line">	public String getHobby() &#123;</span><br><span class="line">		return hobby;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setHobby(String hobby) &#123;</span><br><span class="line">		this.hobby = hobby;</span><br><span class="line">	&#125;</span><br><span class="line">	public String getInfo() &#123;</span><br><span class="line">		return info;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setInfo(String info) &#123;</span><br><span class="line">		this.info = info;</span><br><span class="line">	&#125;</span><br><span class="line">	public Date getBirthday() &#123;</span><br><span class="line">		return birthday;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setBirthday(Date birthday) &#123;</span><br><span class="line">		this.birthday = birthday;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public String toString() &#123;</span><br><span class="line">		return &quot;Student [sid=&quot; + sid + &quot;, sname=&quot; + sname + &quot;, gender=&quot; + gender + &quot;, phone=&quot; + phone + &quot;, hobby=&quot;</span><br><span class="line">				+ hobby + &quot;, info=&quot; + info + &quot;, birthday=&quot; + birthday + &quot;]&quot;;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.service;</span><br><span class="line"></span><br><span class="line">import java.sql.SQLException;</span><br><span class="line">import java.util.List;</span><br><span class="line"></span><br><span class="line">import com.dashucoding.domain.Student;</span><br><span class="line"></span><br><span class="line">/*</span><br><span class="line"> * 这是学生的业务处理规范</span><br><span class="line"> * */</span><br><span class="line">public interface StudentService &#123;</span><br><span class="line">	</span><br><span class="line">	/*</span><br><span class="line">	 * 查询所有学生</span><br><span class="line">	 * list&lt;Student&gt;</span><br><span class="line">	 * */</span><br><span class="line">	List&lt;Student&gt; findAll() throws SQLException;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.service.impl;</span><br><span class="line"></span><br><span class="line">import java.sql.SQLException;</span><br><span class="line">import java.util.List;</span><br><span class="line"></span><br><span class="line">import com.dashucoding.dao.StudentDao;</span><br><span class="line">import com.dashucoding.dao.impl.StudentDaoImpl;</span><br><span class="line">import com.dashucoding.domain.Student;</span><br><span class="line">import com.dashucoding.service.StudentService;</span><br><span class="line">/*</span><br><span class="line"> * 这是学生业务实现</span><br><span class="line"> * */</span><br><span class="line">public class StudentServiceImpl implements StudentService&#123;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public List&lt;Student&gt; findAll() throws SQLException &#123;</span><br><span class="line">		StudentDao dao = new StudentDaoImpl();</span><br><span class="line">		return dao.findAll();</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.servlet;</span><br><span class="line"></span><br><span class="line">import java.io.IOException;</span><br><span class="line">import java.sql.SQLException;</span><br><span class="line">import java.util.List;</span><br><span class="line"></span><br><span class="line">import javax.servlet.ServletException;</span><br><span class="line">import javax.servlet.http.HttpServlet;</span><br><span class="line">import javax.servlet.http.HttpServletRequest;</span><br><span class="line">import javax.servlet.http.HttpServletResponse;</span><br><span class="line"></span><br><span class="line">import com.dashucoding.dao.StudentDao;</span><br><span class="line">import com.dashucoding.dao.impl.StudentDaoImpl;</span><br><span class="line">import com.dashucoding.domain.Student;</span><br><span class="line">import com.dashucoding.service.StudentService;</span><br><span class="line">import com.dashucoding.service.impl.StudentServiceImpl;</span><br><span class="line"></span><br><span class="line">public class StudentListServlet extends HttpServlet &#123;</span><br><span class="line"></span><br><span class="line">	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException &#123;</span><br><span class="line">		try &#123;</span><br><span class="line">			// 查询所有的学生</span><br><span class="line">			StudentService service = new StudentServiceImpl();</span><br><span class="line">			List&lt;Student&gt; list = service.findAll();</span><br><span class="line">			// 把数据存储到作用域中</span><br><span class="line">			request.setAttribute(&quot;list&quot;, list);</span><br><span class="line">			</span><br><span class="line">			// 跳转页面</span><br><span class="line">			request.getRequestDispatcher(&quot;list.jsp&quot;).forward(request,response);</span><br><span class="line">		&#125; catch (SQLException e) &#123;</span><br><span class="line">			// TODO Auto-generated catch block</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException &#123;</span><br><span class="line">		doGet(request, response);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.util;</span><br><span class="line"></span><br><span class="line">import java.io.FileInputStream;</span><br><span class="line">import java.io.InputStream;</span><br><span class="line">import java.sql.Connection;</span><br><span class="line">import java.sql.DriverManager;</span><br><span class="line">import java.sql.ResultSet;</span><br><span class="line">import java.sql.SQLException;</span><br><span class="line">import java.sql.Statement;</span><br><span class="line">import java.util.Properties;</span><br><span class="line"></span><br><span class="line">import javax.sql.DataSource;</span><br><span class="line"></span><br><span class="line">import com.mchange.v2.c3p0.ComboPooledDataSource;</span><br><span class="line"></span><br><span class="line">public class JDBCUtil02 &#123;</span><br><span class="line">	</span><br><span class="line">	static ComboPooledDataSource dataSource = null;</span><br><span class="line"></span><br><span class="line">	static &#123;</span><br><span class="line">		dataSource = new ComboPooledDataSource();</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	public static DataSource getDataSource() &#123;</span><br><span class="line">		return dataSource;</span><br><span class="line">	&#125;</span><br><span class="line">	/**</span><br><span class="line">	 * 获取连接对象</span><br><span class="line">	 * @return</span><br><span class="line">	 * @throws SQLException </span><br><span class="line">	 */</span><br><span class="line">	public static Connection getConn() throws SQLException&#123;</span><br><span class="line">		</span><br><span class="line">		return dataSource.getConnection();</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	/**</span><br><span class="line">	 * 释放资源</span><br><span class="line">	 * @param conn</span><br><span class="line">	 * @param st</span><br><span class="line">	 * @param rs</span><br><span class="line">	 */</span><br><span class="line">	public static void release(Connection conn , Statement st , ResultSet rs)&#123;</span><br><span class="line">		closeRs(rs);</span><br><span class="line">		closeSt(st);</span><br><span class="line">		closeConn(conn);</span><br><span class="line">	&#125;</span><br><span class="line">	public static void release(Connection conn , Statement st)&#123;</span><br><span class="line">		closeSt(st);</span><br><span class="line">		closeConn(conn);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	</span><br><span class="line">	private static void closeRs(ResultSet rs)&#123;</span><br><span class="line">		try &#123;</span><br><span class="line">			if(rs != null)&#123;</span><br><span class="line">				rs.close();</span><br><span class="line">			&#125;</span><br><span class="line">		&#125; catch (SQLException e) &#123;</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;finally&#123;</span><br><span class="line">			rs = null;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	private static void closeSt(Statement st)&#123;</span><br><span class="line">		try &#123;</span><br><span class="line">			if(st != null)&#123;</span><br><span class="line">				st.close();</span><br><span class="line">			&#125;</span><br><span class="line">		&#125; catch (SQLException e) &#123;</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;finally&#123;</span><br><span class="line">			st = null;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	private static void closeConn(Connection conn)&#123;</span><br><span class="line">		try &#123;</span><br><span class="line">			if(conn != null)&#123;</span><br><span class="line">				conn.close();</span><br><span class="line">			&#125;</span><br><span class="line">		&#125; catch (SQLException e) &#123;</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;finally&#123;</span><br><span class="line">			conn = null;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="小结业务逻辑"><a href="#小结业务逻辑" class="headerlink" title="小结业务逻辑"></a>小结业务逻辑</h2><p>先写一个jsp页面,有个链接<code>&lt;a href=&quot;StudentListServlet&quot;&gt;&lt;/a&gt;</code>写个Servlet,接收请求,去调用Service,由service去调用dao,写dao,然后做dao实现,再写Service,做Service的实现,在servlet存储数据,做出页面响应,在list.jsp上显示数据.</p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-b1bfd6455610171a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="dao"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-45251a153841a9d9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="添加效果"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-f609771ec475ec19.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="添加按钮"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot;</span><br><span class="line">	pageEncoding=&quot;UTF-8&quot;%&gt;</span><br><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">&lt;meta charset=&quot;UTF-8&quot;&gt;</span><br><span class="line">&lt;title&gt;添加学生页面&lt;/title&gt;</span><br><span class="line">&lt;/head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">	&lt;form method=&quot;post&quot; action=&quot;AddServlet&quot;&gt;</span><br><span class="line">		&lt;table border=&quot;1&quot; width=&quot;600&quot;&gt;</span><br><span class="line">			&lt;tr&gt;</span><br><span class="line">				&lt;td&gt;姓名&lt;/td&gt;</span><br><span class="line">				&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;sname&quot;&gt;&lt;/td&gt;</span><br><span class="line">			&lt;/tr&gt;</span><br><span class="line">			&lt;tr&gt;</span><br><span class="line">				&lt;td&gt;性别&lt;/td&gt;</span><br><span class="line">				&lt;td&gt;&lt;input type=&quot;radio&quot; name=&quot;gender&quot; value=&quot;男&quot;&gt;男 &lt;input</span><br><span class="line">					type=&quot;radio&quot; name=&quot;gender&quot; value=&quot;女&quot;&gt;女&lt;/td&gt;</span><br><span class="line">			&lt;/tr&gt;</span><br><span class="line">			&lt;tr&gt;</span><br><span class="line">				&lt;td&gt;电话&lt;/td&gt;</span><br><span class="line">				&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;phone&quot;&gt;&lt;/td&gt;</span><br><span class="line">			&lt;/tr&gt;</span><br><span class="line">			&lt;tr&gt;</span><br><span class="line">				&lt;td&gt;生日&lt;/td&gt;</span><br><span class="line">				&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;birthday&quot;&gt;&lt;/td&gt;</span><br><span class="line">			&lt;/tr&gt;</span><br><span class="line">			&lt;tr&gt;</span><br><span class="line">				&lt;td&gt;爱好&lt;/td&gt;</span><br><span class="line">				&lt;td&gt;&lt;input type=&quot;checkbox&quot; name=&quot;hobby&quot; value=&quot;游泳&quot;&gt;游泳 &lt;input</span><br><span class="line">					type=&quot;checkbox&quot; name=&quot;hobby&quot; value=&quot;篮球&quot;&gt;篮球 &lt;input</span><br><span class="line">					type=&quot;checkbox&quot; name=&quot;hobby&quot; value=&quot;足球&quot;&gt;足球 &lt;input</span><br><span class="line">					type=&quot;checkbox&quot; name=&quot;hobby&quot; value=&quot;看书&quot;&gt;看书 &lt;input</span><br><span class="line">					type=&quot;checkbox&quot; name=&quot;hobby&quot; value=&quot;写字&quot;&gt;写字&lt;/td&gt;</span><br><span class="line">			&lt;/tr&gt;</span><br><span class="line">			&lt;tr&gt;</span><br><span class="line">				&lt;td&gt;简介&lt;/td&gt;</span><br><span class="line">				&lt;td&gt;&lt;textarea name=&quot;info&quot; rows=&quot;3&quot; cols=&quot;20&quot;&gt;&lt;/textarea&gt;&lt;/td&gt;</span><br><span class="line">			&lt;/tr&gt;</span><br><span class="line">			&lt;tr&gt;</span><br><span class="line">				&lt;td colspan=&quot;2&quot;&gt;&lt;input type=&quot;submit&quot; value=&quot;添加&quot;&gt;&lt;/td&gt;</span><br><span class="line">			&lt;/tr&gt;</span><br><span class="line">		&lt;/table&gt;</span><br><span class="line">	&lt;/form&gt;</span><br><span class="line">&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.dao;</span><br><span class="line"></span><br><span class="line">import java.sql.SQLException;</span><br><span class="line">import java.util.List;</span><br><span class="line"></span><br><span class="line">import com.dashucoding.domain.Student;</span><br><span class="line"></span><br><span class="line">/*</span><br><span class="line"> * 这是针对学生表的数据访问</span><br><span class="line"> * </span><br><span class="line"> * */</span><br><span class="line">public interface StudentDao &#123;</span><br><span class="line">	</span><br><span class="line">	/*</span><br><span class="line">	 * 查询所有学生</span><br><span class="line">	 * list&lt;Student&gt;</span><br><span class="line">	 * */</span><br><span class="line">	List&lt;Student&gt; findAll() throws SQLException;</span><br><span class="line">	</span><br><span class="line">	void insert(Student student) throws SQLException ;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.dao.impl;</span><br><span class="line"></span><br><span class="line">import java.sql.Connection;</span><br><span class="line">import java.sql.SQLException;</span><br><span class="line">import java.util.List;</span><br><span class="line"></span><br><span class="line">import org.apache.commons.dbutils.QueryRunner;</span><br><span class="line">import org.apache.commons.dbutils.handlers.BeanListHandler;</span><br><span class="line"></span><br><span class="line">import com.dashucoding.dao.StudentDao;</span><br><span class="line">import com.dashucoding.domain.Student;</span><br><span class="line">import com.dashucoding.util.JDBCUtil02;</span><br><span class="line"></span><br><span class="line">/*</span><br><span class="line"> *这是StudentDao的实现,针对前面定义的规范,做出具体的实现</span><br><span class="line"> * */</span><br><span class="line">public class StudentDaoImpl implements StudentDao &#123;</span><br><span class="line">	/*</span><br><span class="line">	 * 查询所有学生</span><br><span class="line">	 */</span><br><span class="line">	@Override</span><br><span class="line">	public List&lt;Student&gt; findAll() throws SQLException &#123;</span><br><span class="line">		QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource());</span><br><span class="line">		return runner.query(&quot;select * from stu&quot;, new BeanListHandler&lt;Student&gt;(Student.class));</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void insert(Student student) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource());</span><br><span class="line">		runner.update(&quot;insert into stu values(null, ?,?,?,?,?,?)&quot;, </span><br><span class="line">				student.getSname(), </span><br><span class="line">				student.getGender(),</span><br><span class="line">				student.getPhone(), </span><br><span class="line">				student.getBirthday(), </span><br><span class="line">				student.getHobby(), </span><br><span class="line">				student.getInfo()</span><br><span class="line">				);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.domain;</span><br><span class="line"></span><br><span class="line">import java.util.Date;</span><br><span class="line"></span><br><span class="line">/*</span><br><span class="line"> * 这是学生封装的对象bean</span><br><span class="line"> * </span><br><span class="line"> * */</span><br><span class="line">public class Student &#123;</span><br><span class="line">	</span><br><span class="line">	private int sid;</span><br><span class="line">	private String sname;</span><br><span class="line">	private String gender;</span><br><span class="line">	private String phone;</span><br><span class="line">	private String hobby;</span><br><span class="line">	private String info;</span><br><span class="line">	private Date birthday;</span><br><span class="line">	</span><br><span class="line">	public Student() &#123;</span><br><span class="line">		super();</span><br><span class="line">		// TODO Auto-generated constructor stub</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public Student(int sid, String sname, String gender, String phone, String hobby, String info, Date birthday) &#123;</span><br><span class="line">		super();</span><br><span class="line">		this.sid = sid;</span><br><span class="line">		this.sname = sname;</span><br><span class="line">		this.gender = gender;</span><br><span class="line">		this.phone = phone;</span><br><span class="line">		this.hobby = hobby;</span><br><span class="line">		this.info = info;</span><br><span class="line">		this.birthday = birthday;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	public Student(String sname, String gender, String phone, String hobby, String info, Date birthday) &#123;</span><br><span class="line">		super();</span><br><span class="line">		this.sname = sname;</span><br><span class="line">		this.gender = gender;</span><br><span class="line">		this.phone = phone;</span><br><span class="line">		this.hobby = hobby;</span><br><span class="line">		this.info = info;</span><br><span class="line">		this.birthday = birthday;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public int getSid() &#123;</span><br><span class="line">		return sid;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setSid(int sid) &#123;</span><br><span class="line">		this.sid = sid;</span><br><span class="line">	&#125;</span><br><span class="line">	public String getSname() &#123;</span><br><span class="line">		return sname;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setSname(String sname) &#123;</span><br><span class="line">		this.sname = sname;</span><br><span class="line">	&#125;</span><br><span class="line">	public String getGender() &#123;</span><br><span class="line">		return gender;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setGender(String gender) &#123;</span><br><span class="line">		this.gender = gender;</span><br><span class="line">	&#125;</span><br><span class="line">	public String getPhone() &#123;</span><br><span class="line">		return phone;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setPhone(String phone) &#123;</span><br><span class="line">		this.phone = phone;</span><br><span class="line">	&#125;</span><br><span class="line">	public String getHobby() &#123;</span><br><span class="line">		return hobby;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setHobby(String hobby) &#123;</span><br><span class="line">		this.hobby = hobby;</span><br><span class="line">	&#125;</span><br><span class="line">	public String getInfo() &#123;</span><br><span class="line">		return info;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setInfo(String info) &#123;</span><br><span class="line">		this.info = info;</span><br><span class="line">	&#125;</span><br><span class="line">	public Date getBirthday() &#123;</span><br><span class="line">		return birthday;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setBirthday(Date birthday) &#123;</span><br><span class="line">		this.birthday = birthday;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public String toString() &#123;</span><br><span class="line">		return &quot;Student [sid=&quot; + sid + &quot;, sname=&quot; + sname + &quot;, gender=&quot; + gender + &quot;, phone=&quot; + phone + &quot;, hobby=&quot;</span><br><span class="line">				+ hobby + &quot;, info=&quot; + info + &quot;, birthday=&quot; + birthday + &quot;]&quot;;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.service;</span><br><span class="line"></span><br><span class="line">import java.sql.SQLException;</span><br><span class="line">import java.util.List;</span><br><span class="line"></span><br><span class="line">import com.dashucoding.domain.Student;</span><br><span class="line"></span><br><span class="line">/*</span><br><span class="line"> * 这是学生的业务处理规范</span><br><span class="line"> * */</span><br><span class="line">public interface StudentService &#123;</span><br><span class="line">	</span><br><span class="line">	/*</span><br><span class="line">	 * 查询所有学生</span><br><span class="line">	 * list&lt;Student&gt;</span><br><span class="line">	 * */</span><br><span class="line">	List&lt;Student&gt; findAll() throws SQLException;</span><br><span class="line">	</span><br><span class="line">	void insert(Student student) throws SQLException ;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.service.impl;</span><br><span class="line"></span><br><span class="line">import java.sql.SQLException;</span><br><span class="line">import java.util.List;</span><br><span class="line"></span><br><span class="line">import com.dashucoding.dao.StudentDao;</span><br><span class="line">import com.dashucoding.dao.impl.StudentDaoImpl;</span><br><span class="line">import com.dashucoding.domain.Student;</span><br><span class="line">import com.dashucoding.service.StudentService;</span><br><span class="line">/*</span><br><span class="line"> * 这是学生业务实现</span><br><span class="line"> * */</span><br><span class="line">public class StudentServiceImpl implements StudentService&#123;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public List&lt;Student&gt; findAll() throws SQLException &#123;</span><br><span class="line">		StudentDao dao = new StudentDaoImpl();</span><br><span class="line">		return dao.findAll();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void insert(Student student) throws SQLException &#123;</span><br><span class="line">		// TODO Auto-generated method stub</span><br><span class="line">		StudentDao dao = new StudentDaoImpl();</span><br><span class="line">		dao.insert(student);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-618effe10e976b32.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="效果"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-c660ad13183515c7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="效果"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line">package com.dashucoding.servlet;</span><br><span class="line"></span><br><span class="line">import java.io.IOException;</span><br><span class="line">import java.text.SimpleDateFormat;</span><br><span class="line">import java.util.Arrays;</span><br><span class="line">import java.util.Date;</span><br><span class="line"></span><br><span class="line">import javax.servlet.ServletException;</span><br><span class="line">import javax.servlet.http.HttpServlet;</span><br><span class="line">import javax.servlet.http.HttpServletRequest;</span><br><span class="line">import javax.servlet.http.HttpServletResponse;</span><br><span class="line"></span><br><span class="line">import com.dashucoding.domain.Student;</span><br><span class="line">import com.dashucoding.service.StudentService;</span><br><span class="line">import com.dashucoding.service.impl.StudentServiceImpl;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * 用于处理学生的添加请求</span><br><span class="line"> */</span><br><span class="line">public class AddServlet extends HttpServlet &#123;</span><br><span class="line"></span><br><span class="line">	protected void doGet(HttpServletRequest request, HttpServletResponse response)</span><br><span class="line">			throws ServletException, IOException &#123;</span><br><span class="line">		request.setCharacterEncoding(&quot;UTF-8&quot;);</span><br><span class="line"></span><br><span class="line">		try &#123;</span><br><span class="line"></span><br><span class="line">			// 1. 获取客户端提交上来的数据</span><br><span class="line">			String sname = request.getParameter(&quot;sname&quot;);</span><br><span class="line">			String gender = request.getParameter(&quot;gender&quot;);</span><br><span class="line">			String phone = request.getParameter(&quot;phone&quot;);</span><br><span class="line">			String birthday = request.getParameter(&quot;birthday&quot;);</span><br><span class="line">			String info = request.getParameter(&quot;info&quot;);</span><br><span class="line">			// String hobby = request.getParameter(&quot;hobby&quot;);//hobby : 游泳，写字， 足球。</span><br><span class="line">			String[] h = request.getParameterValues(&quot;hobby&quot;);</span><br><span class="line"></span><br><span class="line">			String hobby = Arrays.toString(h);</span><br><span class="line">			hobby = hobby.substring(1, hobby.length() - 1);</span><br><span class="line"></span><br><span class="line">			// 2. 添加到数据库</span><br><span class="line">			// string -- date</span><br><span class="line">			Date date = new SimpleDateFormat(&quot;yyyy-MM-dd&quot;).parse(birthday);</span><br><span class="line"></span><br><span class="line">			Student student = new Student(sname, gender, phone, hobby, info, date);</span><br><span class="line">			StudentService service = new StudentServiceImpl();</span><br><span class="line">			service.insert(student);</span><br><span class="line"></span><br><span class="line">			// 3. 跳转到列表页</span><br><span class="line">			request.getRequestDispatcher(&quot;StudentListServlet&quot;).forward(request, response);</span><br><span class="line"></span><br><span class="line">		&#125; catch (Exception e) &#123;</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	protected void doPost(HttpServletRequest request, HttpServletResponse response)</span><br><span class="line">			throws ServletException, IOException &#123;</span><br><span class="line">		doGet(request, response);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>要的jar包,都在这.</p>
<p><img src="https://upload-images.jianshu.io/upload_images/11158618-9181932d3b49f07d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="群里"></p>
<h2 id="结言"><a href="#结言" class="headerlink" title="结言"></a>结言</h2><p>好了，欢迎在留言区留言，与大家分享你的经验和心得。</p>
<p>感谢你学习今天的内容，如果你觉得这篇文章对你有帮助的话，也欢迎把它分享给更多的朋友，感谢。</p>
<blockquote>
<p>达叔小生：往后余生，唯独有你<br>You and me, we are family !<br>90后帅气小伙，良好的开发习惯；独立思考的能力；主动并且善于沟通<br>简书博客： 达叔小生<br><a href="https://www.jianshu.com/u/c785ece603d1" target="_blank" rel="noopener">https://www.jianshu.com/u/c785ece603d1</a></p>
</blockquote>
<h2 id="结语"><a href="#结语" class="headerlink" title="结语"></a>结语</h2><ul>
<li>下面我将继续对 其他知识 深入讲解 ，有兴趣可以继续关注</li>
<li>小礼物走一走 or 点赞</li>
</ul>
      
    </div>
    
    
    

      <div>
        
          <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>
        
      </div>

    

      <div>
        <div id="wechat_subscriber" style="display: block; padding: 10px 0; margin: 20px auto; width: 100%; text-align: center">
    <img id="wechat_subscriber_qcode" src="/images/微信公众号.jpg" alt="达叔小生 wechat" style="width: 200px; max-width: 100%">
    <div>欢迎您扫一扫上面的微信公众号，订阅我的博客！</div>
</div>

      </div>
    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechatpay.png" alt="达叔小生 微信支付">
        <p>微信支付</p>
      </div>
    

    

    

  </div>
</div>

      </div>
    

    
      <div>
        <ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者：</strong>
    达叔小生
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://huangguangda.github.io/2019/01/30/1/" title="第80节:Java中的MVC设计模式">https://huangguangda.github.io/2019/01/30/1/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>
    本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/3.0/" rel="external nofollow" target="_blank">CC BY-NC-SA 3.0</a> 许可协议。转载请注明出处！
  </li>
</ul>

      </div>
    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/带你学会Java基础课程/" rel="tag"><i class="fa fa-tag"></i> 带你学会Java基础课程</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/01/29/1/" rel="next" title="第79节:Java中的一些要点">
                <i class="fa fa-chevron-left"></i> 第79节:Java中的一些要点
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/01/31/1/" rel="prev" title="第81节：Java中的数组">
                第81节：Java中的数组 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
        
<script>
  with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='//bdimg.share.baidu.com/static/api/js/share.js?cdnversion='+~(-new Date()/36e5)];
</script>

      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zNTU5OC8xMjEzNA=="></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
      <div id="sidebar-dimmer"></div>
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/avatar.gif" alt="达叔小生">
            
              <p class="site-author-name" itemprop="name">达叔小生</p>
              <p class="site-description motion-element" itemprop="description"></p>

            <div id="days"></div>
<script>
function show_date_time(){
window.setTimeout("show_date_time()", 1000);
BirthDay=new Date("04/23/1997 00:00:00");
today=new Date();
timeold=(today.getTime()-BirthDay.getTime());
sectimeold=timeold/1000
secondsold=Math.floor(sectimeold);
msPerDay=24*60*60*1000
e_daysold=timeold/msPerDay
daysold=Math.floor(e_daysold);
e_hrsold=(e_daysold-daysold)*24;
hrsold=setzero(Math.floor(e_hrsold));
e_minsold=(e_hrsold-hrsold)*60;
minsold=setzero(Math.floor((e_hrsold-hrsold)*60));
seconds=setzero(Math.floor((e_minsold-minsold)*60));
document.getElementById('days').innerHTML="已运行"+daysold+"天"+hrsold+"小时"+minsold+"分"+seconds+"秒";
}
function setzero(i){
if (i<10)
{i="0" + i};
return i;
}
show_date_time();
</script>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">94</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">20</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">21</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
                <a title="收藏到书签，偶尔High一下^_^" rel="alternate" class="mw-harlem_shake_slow wobble shake" href="javascript:(function() {
    function c() {
        var e = document.createElement(&quot;link&quot;);
        e.setAttribute(&quot;type&quot;, &quot;text/css&quot;);
        e.setAttribute(&quot;rel&quot;, &quot;stylesheet&quot;);
        e.setAttribute(&quot;href&quot;, f);
        e.setAttribute(&quot;class&quot;, l);
        document.body.appendChild(e)
    }
 
    function h() {
        var e = document.getElementsByClassName(l);
        for (var t = 0; t < e.length; t++) {
            document.body.removeChild(e[t])
        }
    }
 
    function p() {
        var e = document.createElement(&quot;div&quot;);
        e.setAttribute(&quot;class&quot;, a);
        document.body.appendChild(e);
        setTimeout(function() {
            document.body.removeChild(e)
        }, 100)
    }
 
    function d(e) {
        return {
            height : e.offsetHeight,
            width : e.offsetWidth
        }
    }
 
    function v(i) {
        var s = d(i);
        return s.height > e && s.height < n && s.width > t && s.width < r
    }
 
    function m(e) {
        var t = e;
        var n = 0;
        while (!!t) {
            n += t.offsetTop;
            t = t.offsetParent
        }
        return n
    }
 
    function g() {
        var e = document.documentElement;
        if (!!window.innerWidth) {
            return window.innerHeight
        } else if (e && !isNaN(e.clientHeight)) {
            return e.clientHeight
        }
        return 0
    }
 
    function y() {
        if (window.pageYOffset) {
            return window.pageYOffset
        }
        return Math.max(document.documentElement.scrollTop, document.body.scrollTop)
    }
 
    function E(e) {
        var t = m(e);
        return t >= w && t <= b + w
    }
 
    function S() {
        var e = document.createElement(&quot;audio&quot;);
        e.setAttribute(&quot;class&quot;, l);
        e.src = i;
        e.loop = false;
        e.addEventListener(&quot;canplay&quot;, function() {
            setTimeout(function() {
                x(k)
            }, 500);
            setTimeout(function() {
                N();
                p();
                for (var e = 0; e < O.length; e++) {
                    T(O[e])
                }
            }, 15500)
        }, true);
        e.addEventListener(&quot;ended&quot;, function() {
            N();
            h()
        }, true);
        e.innerHTML = &quot; <p>If you are reading this, it is because your browser does not support the audio element. We recommend that you get a new browser.</p> <p>&quot;;
        document.body.appendChild(e);
        e.play()
    }
 
    function x(e) {
        e.className += &quot; &quot; + s + &quot; &quot; + o
    }
 
    function T(e) {
        e.className += &quot; &quot; + s + &quot; &quot; + u[Math.floor(Math.random() * u.length)]
    }
 
    function N() {
        var e = document.getElementsByClassName(s);
        var t = new RegExp(&quot;\\b&quot; + s + &quot;\\b&quot;);
        for (var n = 0; n < e.length; ) {
            e[n].className = e[n].className.replace(t, &quot;&quot;)
        }
    }
 
    var e = 30;
    var t = 30;
    var n = 350;
    var r = 350;
    var i = &quot;//s3.amazonaws.com/moovweb-marketing/playground/harlem-shake.mp3&quot;;
    var s = &quot;mw-harlem_shake_me&quot;;
    var o = &quot;im_first&quot;;
    var u = [&quot;im_drunk&quot;, &quot;im_baked&quot;, &quot;im_trippin&quot;, &quot;im_blown&quot;];
    var a = &quot;mw-strobe_light&quot;;
    var f = &quot;//s3.amazonaws.com/moovweb-marketing/playground/harlem-shake-style.css&quot;;
    var l = &quot;mw_added_css&quot;;
    var b = g();
    var w = y();
    var C = document.getElementsByTagName(&quot;*&quot;);
    var k = null;
    for (var L = 0; L < C.length; L++) {
        var A = C[L];
        if (v(A)) {
            if (E(A)) {
                k = A;
                break
            }
        }
    }
    if (A === null) {
        console.warn(&quot;Could not find a node of the right size. Please try a different page.&quot;);
        return
    }
    c();
    S();
    var O = [];
    for (var L = 0; L < C.length; L++) {
        var A = C[L];
        if (v(A)) {
            O.push(A)
        }
    }
})()"><i class="fa fa-music"></i> High</a>

            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/huangguangda" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:2397923107@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.jianshu.com/u/c785ece603d1" target="_blank" title="简书">
                      
                        <i class="fa fa-fw fa-android"></i>简书</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://blog.csdn.net/qq_36232611" target="_blank" title="csdn">
                      
                        <i class="fa fa-fw fa-spinner"></i>csdn</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.douban.com/people/186539024/" target="_blank" title="豆瓣">
                      
                        <i class="fa fa-fw fa-bath"></i>豆瓣</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.zhihu.com/people/da-shu-xiao-sheng/activities" target="_blank" title="知乎">
                      
                        <i class="fa fa-fw fa-globe"></i>知乎</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://juejin.im/user/5a16e1f3f265da43128096cb" target="_blank" title="掘金">
                      
                        <i class="fa fa-fw fa-google"></i>掘金</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://weibo.com/5507262458/profile?topnav=1&wvr=6" target="_blank" title="微博">
                      
                        <i class="fa fa-fw fa-weibo"></i>微博</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzI1NTcxOTQ1Nw==&scene=124#wechat_redirect" target="_blank" title="历史">
                      
                        <i class="fa fa-fw fa-apple"></i>历史</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="http://www.cnblogs.com/dashucoding/" target="_blank" title="博客园">
                      
                        <i class="fa fa-fw fa-facebook"></i>博客园</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://gdone.gitee.io/englishpages/index.html" target="_blank" title="英文简历">
                      
                        <i class="fa fa-fw fa-globe"></i>英文简历</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://gdone.gitee.io/pages/" target="_blank" title="中文简历">
                      
                        <i class="fa fa-fw fa-heartbeat"></i>中文简历</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://gitee.com/GDone/events" target="_blank" title="码云">
                      
                        <i class="fa fa-fw fa-twitter"></i>码云</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://my.oschina.net/xiaomaomi1997/" target="_blank" title="开源中国">
                      
                        <i class="fa fa-fw fa-facebook"></i>开源中国</a>
                  </span>
                
            </div>
          

          
          
            <div class="cc-license motion-element" itemprop="license">
              <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" target="_blank">
                <img src="/images/cc-by-nc-sa.svg" alt="Creative Commons">
              </a>
            </div>
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-inline">
              <div class="links-of-blogroll-title">
                <i class="fa fa-fw fa-globe"></i>
                推荐阅读
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.alloyteam.com/nav/" title="Web前端导航" target="_blank">Web前端导航</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.36zhen.com/t?id=3448" title="前端书籍资料" target="_blank">前端书籍资料</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://ife.baidu.com/" title="百度前端技术学院" target="_blank">百度前端技术学院</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://wf.uisdc.com/cn/" title="google前端开发基础" target="_blank">google前端开发基础</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.geekbang.org/" title="极客邦科技" target="_blank">极客邦科技</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://ilxdh.com/" title="龙轩导航" target="_blank">龙轩导航</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://code.giffox.com/" title="程序员导航" target="_blank">程序员导航</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://search.chongbuluo.com/" title="快搜" target="_blank">快搜</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.android-doc.com/training/index.html" title="Android中文API" target="_blank">Android中文API</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://developer.android.google.cn/" title="Android Developers" target="_blank">Android Developers</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.freecodecamp.cn/" title="FreeCodeCamp中文社区" target="_blank">FreeCodeCamp中文社区</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://docs.oracle.com/javase/specs/" title="Java SE规范" target="_blank">Java SE规范</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://liuwangshu.cn/" title="刘望舒" target="_blank">刘望舒</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.colabug.com/author/3159ee71c93b3b4a/" title="小楼昨夜又秋风" target="_blank">小楼昨夜又秋风</a>
                  </li>
                
              </ul>

            </div>
          


        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#学生管理系统"><span class="nav-number">2.</span> <span class="nav-text">学生管理系统</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#小结业务逻辑"><span class="nav-number">3.</span> <span class="nav-text">小结业务逻辑</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#结言"><span class="nav-number">4.</span> <span class="nav-text">结言</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#结语"><span class="nav-number">5.</span> <span class="nav-text">结语</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="heart">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">达叔小生</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">357.6k</span>
  
</div>






  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.4</div>




<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共357.6k字</span>
</div>

<div class="weixin-box">
  <div class="weixin-menu">
    <div class="weixin-hover">
      <div class="weixin-description">微信扫一扫，订阅本博客</div>
    </div>
  </div>
</div>
        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user">本站访客数</i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye">本站总访问量</i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>





  <script type="text/javascript">
    (function() {
      var hm = document.createElement("script");
      hm.src = "//tajs.qq.com/stats?sId=65809965";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  



  
  









  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  

  
  
    <script type="text/javascript" src="/lib/three/three.min.js"></script>
  

  
  
    <script type="text/javascript" src="/lib/three/three-waves.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  












  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  
  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  


  <script type="text/javascript" src="/lib/clipboard/clipboard.min.js"></script>
<script type="text/javascript" src="/js/src/custom.js"></script>


   <canvas class="fireworks" style="position: fixed;left: 0;top: 0;z-index: 1; pointer-events: none"></canvas> 
   <script type="text/javascript" src="//cdn.bootcss.com/animejs/2.2.0/anime.min.js"></script> 
   <script type="text/javascript" src="/js/src/fireworks.js"></script>


  <!-- 代码块复制功能 -->
  <script type="text/javascript" src="/js/src/clipboard.min.js"></script>  
  <script type="text/javascript" src="/js/src/clipboard-use.js"></script>
<script src="/live2dw/lib/L2Dwidget.min.js?0c58a1486de42ac6cc1c59c7d98ae887"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"live2d-widget-model-hibiki"},"display":{"position":"right","width":75,"height":150},"mobile":{"show":false},"log":false});</script></body>
</html>